<!-- src/components/admin/Index.vue -->
<script setup>
import {computed, ref} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import {ArrowDown, Document, HomeFilled, User} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

// 控制侧边栏是否折叠
const isCollapse = ref(false)

// 切换侧边栏折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 当前激活的菜单项（可以根据路由 path 来动态设置）
const activeMenu = computed(() => route.path)

// 面包屑导航（简单示例，您可以根据路由层级动态生成）
const breadcrumb = computed(() => {
  const path = route.path
  if (path === '/') return '系统首页'
  if (path.includes('/info-manage')) return '信息管理'
  if (path.includes('/user-manage')) return '用户管理'
  return '首页'
})

// 退出登录（示例功能，您可以根据需要实现）
const logout = () => {
  // 清除 token 或用户信息
  localStorage.removeItem('token')
  // 跳转到登录页（您需要配置对应的路由）
  router.push('/login')
}
</script>

<template>
  <div class="admin-layout">
    <!-- ========== 顶部导航栏 ========== -->
    <header class="top-navbar">
      <div class="navbar-left">
        <h1 class="system-title">后台管理</h1>
      </div>
      <div class="navbar-right">
        <el-dropdown trigger="click">
          <span class="el-dropdown-link">
            <el-avatar class="user-avatar" icon="UserFilled" size="small"></el-avatar>
            <span>管理员</span>
            <el-icon class="el-icon--right"><ArrowDown/></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>个人中心</el-dropdown-item>
              <el-dropdown-item>设置</el-dropdown-item>
              <el-dropdown-item divided @click="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>

    <!-- ========== 主体布局：侧边栏 + 内容区 ========== -->
    <div class="main-container">
      <!-- ========== 左侧菜单栏 ========== -->
      <aside :class="{ collapsed: isCollapse }" class="sidebar">
        <el-menu
            :collapse="isCollapse"
            :default-active="activeMenu"
            active-text-color="#409EFF"
            background-color="#304156"
            class="el-menu-vertical"
            router
            text-color="#bfcbd9"
        >
          <el-menu-item index="/">
            <el-icon>
              <HomeFilled/>
            </el-icon>
            <template #title>系统首页</template>
          </el-menu-item>

          <el-sub-menu index="/info-manage">
            <template #title>
              <el-icon>
                <Document/>
              </el-icon>
              <span>信息管理</span>
            </template>
            <el-menu-item index="/info-manage/classify">分类信息</el-menu-item>
            <el-menu-item index="/info-manage/search">搜索信息</el-menu-item>
          </el-sub-menu>

          <el-sub-menu index="/user-manage">
            <template #title>
              <el-icon>
                <User/>
              </el-icon>
              <span>用户管理</span>
            </template>
            <el-menu-item index="/user-manage/admin">管理员信息</el-menu-item>
            <el-menu-item index="/user-manage/user">普通用户信息</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </aside>

      <!-- ========== 右侧内容区 ========== -->
      <main class="content-wrapper">
        <!-- ========== 内容头部：面包屑导航 ========== -->
        <div class="content-header">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ breadcrumb }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>

        <!-- ========== 主内容区：通过 Vue Router 动态渲染页面 ========== -->
        <div class="content-body">
          <router-view/>
        </div>
      </main>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.admin-layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ========== 顶部导航栏样式 ========== */
.top-navbar {
  height: 50px;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 100;

  .system-title {
    margin: 0;
    font-size: 18px;
    font-weight: bold;
    color: #333;
  }

  .navbar-right {
    display: flex;
    align-items: center;
  }

  .user-avatar {
    margin-left: 8px;
    background-color: #409EFF;
  }

  .el-dropdown-link {
    display: flex;
    align-items: center;
    cursor: pointer;
    color: #606266;
  }
}

/* ========== 主体容器样式 ========== */
.main-container {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* ========== 左侧菜单栏样式 ========== */
.sidebar {
  width: 220px;
  background-color: #304156;
  height: 100%;
  overflow-y: auto;
  transition: width 0.2s;

  &.collapsed {
    width: 64px;

    .el-menu {
      width: 64px;

      ::v-deep(.el-sub-menu__title span),
      ::v-deep(.el-menu-item span) {
        display: none;
      }

      ::v-deep(.el-sub-menu__icon-arrow) {
        display: none;
      }
    }
  }
}

/* 菜单样式 */
.el-menu-vertical {
  height: 100%;
  border-right: none;
  width: 220px;

  ::v-deep(.el-menu-item) {
    height: 50px;
    line-height: 50px;
    margin: 0;
  }

  ::v-deep(.el-sub-menu__title) {
    height: 50px;
    line-height: 50px;
    margin: 0;
  }
}

/* ========== 右侧内容区样式 ========== */
.content-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.content-header {
  padding: 15px 20px;
  background-color: #fff;
  border-bottom: 1px solid #eee;
}

.content-body {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background-color: #f0f2f5;
}
</style>